//////////////////////////////////////////////////////////////////////////////////////////////
//	File			:	"MapManager.cpp"
//
//	Author			:	Mike Davis, Matt Stover, James Thornton, Keith Webster
//	
//	Project			:	TacWars
//
//	Last Modified	:	11/6/2013
//
//	Purpose			:	
//
//////////////////////////////////////////////////////////////////////////////////////////////
#include "../TinyXML/tinyxml.h"
#include "MapManager.h"
#include "BattleMap.h"
#include "../../SGD Wrappers/CSGD_TextureManager.h"
#include "../../SGD Wrappers/SGD_String.h"
MapManager* MapManager::GetInstance(void)
{
	static MapManager s_Instance;
	return &s_Instance;
}
MapManager::MapManager()
{

}
MapManager::~MapManager()
{
}
bool MapManager::Load(const char* szFileName)
{
	int width;
	int height;
	TiXmlDocument doc;
	if(doc.LoadFile(szFileName) == false)
		return false;
	TiXmlElement* Root = doc.RootElement();
	if(Root == nullptr)
		return false;
	Tile.clear();
	TiXmlElement *mapData = Root->FirstChildElement("Level");
	mapData->Attribute("SizeY",&height);
	mapData->Attribute("SizeX",&width);
	TiXmlElement *tileData = mapData->FirstChildElement("Tile");
	while(tileData != nullptr)
	{
		TileM* temp = new TileM;
		int pos;
		int flag;
		//int type;
		int node;
		int high;
		int move;
		int defense;
		int collision;
		int rectx;
		int recty;
		int locx;
		int locy;
		int start;
		tileData->Attribute("Node",&node);
		temp->isNode = (node == 1 ? true : false);
		tileData->Attribute("HighGround",&high);
		temp->isHigh = (high == 1 ? true : false);
		tileData->Attribute("PosX",&pos);
		temp->position.x = pos;
		tileData->Attribute("PosY",&pos);
		temp->position.y = pos;
		tileData->Attribute("Flag",&flag);
		temp->isFlag = (flag == 1 ? true : false);
		temp->FlagStart = ( flag == 1 ? true : false);
		//tileData->Attribute("TerrainType",&type);
		//switch(type)
		//{
		//case 0:
		//	temp->Type = Grass;
		//	break;
		//case 1:
		//	temp->Type = Road;
		//	break;
		//case 2:
		//	temp->Type = Bunker;
		//	break;
		//case 3:
		//	temp->Type = Forest;
		//	break;
		//case 4:
		//	temp->Type = High_Ground;
		//	break;
		//case 5:
		//	temp->Type = Swamp;
		//	break;
		//case 6:
		//	temp->Type = Power_Node;
		//	break;
		//}
		tileData->Attribute("Move",&move);
		temp->moveAmount = move;
		tileData->Attribute("Defense",&defense);
		temp->defenseBonus = defense;
		tileData->Attribute("Collision",&collision);
		temp->isCollide = (collision == 1 ? true : false);
		tileData->Attribute("TileWidth",&rectx);
		temp->srcRect.right = rectx;
		tileData->Attribute("TileHeight",&recty);
		temp->srcRect.bottom = recty;
		tileData->Attribute("TileRectX",&locx);
		temp->srcRect.left = locx;
		tileData->Attribute("TileRectY",&locy);
		temp->srcRect.top = locy;
		tileData->Attribute("Start",&start);
		temp->startArea = (start == 1 ? true : false);
		TiXmlElement* file = tileData->FirstChildElement("File");
		string filename = file->GetText();
		wchar_t f[200];
		size_t num = strlen(filename.c_str())+1;
		CSTR_TO_TSTR(f,num,filename.c_str());
			TSTRING path = _T("resource/Images/");
			TSTRING filepath = path + f;
		temp->ImageId = CSGD_TextureManager::GetInstance()->LoadTexture((filepath.c_str()));
		Tile.push_back(*temp);
		tileData = tileData->NextSiblingElement("Tile");
	}
	map = new BattleMap();
	map->CreateBattleMap(Tile,width,height);
	return (Tile.size() > 0);
}
void MapManager::Unload()
{
}